package view.mediator.multidevs
{
	import com.adobe.utils.ArrayUtil;
	
	import controllers.commands.QueryDevicesCommand;
	import controllers.managers.SingletonManager;
	import controllers.managers.UIInstanceManager;
	
	import event.DeviceActionEvent;
	
	import flash.events.MouseEvent;
	
	import model.assets.AssetsModel;
	import model.context.DeviceMonitorContext;
	import model.context.base.IContext;
	import model.registry.EnvConst;
	
	import mx.collections.ArrayCollection;
	import mx.collections.ArrayList;
	import mx.controls.Alert;
	import mx.controls.DataGrid;
	import mx.core.UIComponent;
	import mx.olap.QueryCubeBuilder;
	import mx.utils.ArrayUtil;
	
	import utils.ArrayListUtils;
	import utils.DialogUtil;
	
	import view.ListPanel;
	import view.manager.GlobalEventDispatcher;
	import view.mediator.base.IMediator;
	import view.panel.DeviceSelectionPanel;
	import view.panel.MultiDeviceMonitor;
	
	public class DeviceSelectionMediator implements IMediator
	{
		private var deviceSelectorPanel:DeviceSelectionPanel = null;
		private var listPanel:ListPanel = null;
		private var deviceMonitorPanel:MultiDeviceMonitor=null;
		
		public function DeviceSelectionMediator()
		{
		}
		
		public function setView(view:UIComponent):void
		{
			initView(view);
			initEvents();
		}
		
		private function initView(view:UIComponent):void
		{
			deviceSelectorPanel = DeviceSelectionPanel(view);
			listPanel = ListPanel(UIInstanceManager.getUI(ListPanel.NAME));
			deviceMonitorPanel=MultiDeviceMonitor(UIInstanceManager.getUI(MultiDeviceMonitor.NAME));	
		}
		
		//初始化事件
		public function initEvents():void
		{
			deviceSelectorPanel.btnComplete.addEventListener(MouseEvent.CLICK, onMonitorPageHandler);
			GlobalEventDispatcher.addEventListener(DeviceActionEvent.DB_CLICK_DEV_ITEM, onDBClickDevItemHandler);
			deviceSelectorPanel.chkAllDevs.addEventListener(MouseEvent.CLICK, onSelectAllDeviceHandler);
			deviceSelectorPanel.btnAddDev.addEventListener(MouseEvent.CLICK, onAddDeviceHandler);
			
		}
		
		/**
		 *  添加指定的设备
		 **/
		private function onAddDeviceHandler(evt:MouseEvent):void
		{
			var listPanel:ListPanel = ListPanel(UIInstanceManager.getUI(ListPanel.NAME));
			var devListDS : ArrayCollection = ArrayCollection(listPanel.getDataSource());
			var targetListDS : ArrayCollection = deviceSelectorPanel.getDataProvider();
			
			var devID:String = deviceSelectorPanel.txtDevText.text; 
			if ( devID != "")
			{
				var newItem:Object = getDevById(devID, devListDS);
				if (  existDevId(devID, devListDS) )
				{
					if ( !ArrayListUtils.containsValue(targetListDS, newItem) )
					{
						deviceSelectorPanel.addItem(newItem);	
					}
					else
					{
						DialogUtil.openWarning("设备已经存在设备选择列表中.");
					}
				}
				else
				{
					DialogUtil.openWarning("设备不存在.");				
				}
			}
			else
			{
				DialogUtil.openWarning("请填入设备编码.");
			}
		}
		
		/**
		 *  是否存在设备
		 **/
		private function existDevId(id:String, list:Object):Boolean
		{
			for each(var item:Object in list)
			{
				if(item.sn == id)
				{
					return true;
				}
			}
			return false;
		}
		
		/**
		 *  根据设备ID获取设备.
		 **/
		private function getDevById(id:String, list:Object):Object
		{
			for each(var item:Object in list)
			{
				if(item.sn == id)
				{
					return item;
				}
			}
			return null;
		}
		
		/**
		 * 选择所有设备
		 **/
		private function onSelectAllDeviceHandler(evt:MouseEvent):void
		{
			//将相关数据源带过去
			//1.take all source datasource
			//2.filter exist. add ....
			var listPanel:ListPanel = ListPanel(UIInstanceManager.getUI(ListPanel.NAME));
			var sourceDS : Object = listPanel.getDataSource();
			var targetDS : ArrayCollection = deviceSelectorPanel.getDataProvider();
			
			//将设备列表插入设备选择列表中
			for each(var item:Object in sourceDS)
			{
				//不插入存在的设备.
				if ( !existDevId(item.sn, targetDS) )
				{
					deviceSelectorPanel.addItem(item);
				}
			}
		}
		
		/**
		 * 完成选择,进入监控界面
		 **/
		private function onMonitorPageHandler(evt:MouseEvent):void
		{
			deviceSelectorPanel.visible = false;
			listPanel = ListPanel(UIInstanceManager.getUI(ListPanel.NAME));
			deviceMonitorPanel=MultiDeviceMonitor(UIInstanceManager.getUI(MultiDeviceMonitor.NAME));
			//进入设备选择状态
			listPanel.setWorkState();
			deviceMonitorPanel.visible=true;
			
			var context : DeviceMonitorContext = new DeviceMonitorContext();
			context.deviceItem = deviceSelectorPanel.getDataProvider();
			context.targetPanel = deviceMonitorPanel;
			
			//将命令缓存起来，供手动调用最后一次操作
			//执行命令
			var cmd:QueryDevicesCommand = new QueryDevicesCommand(context);
			SingletonManager.setServ(QueryDevicesCommand.NAME, cmd);
			cmd.execute();
		}
		
		/**
		 *  双击设备
		 **/
		private function onDBClickDevItemHandler(evt:DeviceActionEvent):void
		{
			var item:Object  = evt.dataItem;
			if ( item != null 
				&& !ArrayListUtils.containsValue(deviceSelectorPanel.getDataProvider(), item) 
				&& !isOverMaxSize(deviceSelectorPanel.getDataProvider()))
			{
				deviceSelectorPanel.addItem(item);
				item.sid = deviceSelectorPanel.getDataProvider().length;
			}
		}
		
		/**
		 *  检查是否超过最大值
		 **/
		private function isOverMaxSize(list:ArrayCollection):Boolean
		{
			if ( list.length > EnvConst.MAX_TOTAL_MONITOR_DEVICE )
			{
				DialogUtil.openWarning("超过最大监控设备台数");							
				return true;
			}
			else
			{
				return false;
			}
		}
		
		public function setContext(context:IContext):void
		{
		}
	}
}